package cn.jxau.fukin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jxau.fukin.dao.CardMapper;
import cn.jxau.fukin.dao.TradeMapper;
import cn.jxau.fukin.dao.UserMapper;
import cn.jxau.fukin.model.Card;
import cn.jxau.fukin.model.CardExample;
import cn.jxau.fukin.model.CardUser;
import cn.jxau.fukin.model.Trade;
import cn.jxau.fukin.model.User;
import cn.jxau.fukin.model.UserExample;
import cn.jxau.fukin.model.UserExample.Criteria;
import cn.jxau.fukin.service.ICardService;

@Service("cardService")
public class CardService implements ICardService {
	@Autowired
	CardMapper cardMapper;
	@Autowired
	UserMapper userMapper;
	@Autowired
	TradeMapper tradeMapper;

	@Override
	public List<CardUser> queryCardInfo(User user) {
		if (user.getuName() != null && user.getuName().trim() != "") {
			UserExample userExample = new UserExample();
			Criteria criteria = userExample.createCriteria();
			criteria.andUNameEqualTo(user.getuName());
			List<User> userList = userMapper.selectByExample(userExample);
			CardExample cardExample = new CardExample();
			cardExample.createCriteria().andCUIdEqualTo(userList.get(0).getuId());
			List<Card> cardList = cardMapper.selectByExample(cardExample);
			Trade trade = tradeMapper.selectLeastTradeTime(userList.get(0).getuId());
			ArrayList<CardUser> cardUserList = new ArrayList<CardUser>();
			for (Card card : cardList) {
				CardUser cardUser = new CardUser();
				cardUser.setCard(card);
				cardUser.setTrade(trade);
				cardUser.setUser(userList.get(0));
				cardUserList.add(cardUser);
			}
			return cardUserList;
		}

		return null;
	}

}
